function graphsVec=plotTransitions(pathMat,inStru,nameStru,flagNoLegend)
% =========================================================================
%% plotTransitions(varMat,obsnames,shonames,title,offset);
%
% Given and [Ny Nper Nmod] matrix of transition paths
% Plot them for each series across different models 
% 
%% Inputs
% *pathMat*    [Ny Nper Nmod] matrix of SS transitions 
%
% *inStru*     Structure with all inputs for the graph, 
%              including 
%              .NFigCols     Columns per figure (default is 2) 
%              .NFigRows     Rows per figure (default is min(4,Ny/2) ) 
%
% *nameStru*    .rows
%               .pages
%
%
%% Outputs 
% graphsVec: Vector handles 
%
% Alejandro Justiniano January 21st 2010
% =========================================================================
if nargin < 4 || isempty(flagNoLegend) 
    flagNoLegend=0; 
end 
[Nstates Nper Nmod]=size(pathMat); 
%% 1. Defaults
if isempty(inStru)==true 
    inStru.flagEmpty=true; 
end 
[~,inStru]=ch_field(inStru,'flagColor',1); 
[~,inStru]=ch_field(inStru,'colors',[0 0 1;0 0 0;1 0 0;0 0.5 0;0.55 0.55 0.55]);
[~,inStru]=ch_field(inStru,'lineStyles',{'-','-.','--','--','-'}); 
[~,inStru]=ch_field(inStru,'markers',{'none','none','^','none','*','s'}); 
[~,inStru]=ch_field(inStru,'markersSize',[1 1.5 0.5 4 4.5]); 
[~,inStru]=ch_field(inStru,'xOffset',1e-5); 
[~,inStru]=ch_field(inStru,'YGridOn',0); 
[~,inStru]=ch_field(inStru,'XGridOn',0); 
[~,inStru]=ch_field(inStru,'yOffset',0.005); 
[~,inStru]=ch_field(inStru,'flagBoxOff',1); 
if Nmod > 3
    [~,inStru]=ch_field(inStru,'lineWidths',[1.5 1.5 2.25 1 1]);
else
    [~,inStru]=ch_field(inStru,'lineWidths',[1.5 2 2.5]);
end
[~,inStru]=ch_field(inStru,'suptitle',[]); 
[~,inStru]=ch_field(inStru,'suptitleFlush','left'); 
[~,inStru]=ch_field(inStru,'xaxis',(0:Nper-1)'); 
[~,inStru]=ch_field(inStru,'paperPosition',[0.0 0.0 8.5 11]); 
[~,inStru]=ch_field(inStru,'legendFontSize',14); 
[~,inStru]=ch_field(inStru,'flagLandscape',0); 
% NRows and NCols defined below 
%% Requires \, asking to replace '\\' with '\'
nameStru.rows=regexprep(nameStru.rows,'\$',''); 
nameStru.rows=regexprep(nameStru.rows,'{',''); 
nameStru.rows=regexprep(nameStru.rows,'}',''); 
nameStru.rows=regexprep(nameStru.rows,'\\\','\'); 

nameStru.pages=regexprep(nameStru.pages,'\$',''); 
nameStru.pages=regexprep(nameStru.pages,'\\\','\'); 
nameStru.pages=regexprep(nameStru.pages,'{',''); 
nameStru.pages=regexprep(nameStru.pages,'}',''); 

if flagNoLegend~=1
    [~,inStru]=ch_field(inStru,'legend',nameStru.pages);
end

inStru=plotAssignDefaults(inStru);
%% 2. Check sizes
if length(nameStru.rows)~=Nstates; 
    error('nameStru.rows does not match number of rows in pathMat'); 
end 
if length(nameStru.pages)~=Nmod; 
    error('nameStru.pages does not match number of pages in pathMat'); 
end 
% if isfield(inStru,'legend')==false; 
%     error('Need provide inStru.legend'); 
% end 
%% 3. Organization of Plots
[~,inStru]=ch_field(inStru,'NFigCols',2); 
[~,inStru]=ch_field(inStru,'NFigRows',min(4,ceil(Nstates/inStru.NFigCols))); 
if ~isempty(inStru.axesFontSize); 
    set(0,'DefaultAxesFontSize',inStru.axesFontSize);
end 

%% 4. Graph Settings 
NOuter=Nstates; 
NRows=inStru.NFigRows;
NCols=inStru.NFigCols;

graphsVec =zeros(ceil(NOuter/(NRows*NCols)),1);

ISubplots=0;
IGraphs  =0;
ICounter =0;

for jj=1:NOuter; 
        
    if ISubplots==0
        IGraphs=IGraphs+1;
        graphsVec(IGraphs,1)=figure;
    end
    ISubplots=ISubplots+1;
    ICounter =ICounter+1;
    
    subplot(NRows,NCols,ISubplots);
    
    if ISubplots==NRows*NCols || jj==NOuter
        if flagNoLegend~=1
            inStru.flagNoLegend=0;
        end
    else
        inStru.flagNoLegend=1;        
    end

    % Nper Nmod matrix 
    tempMat=squeeze(pathMat(jj,:,:));    
    tempTit=nameStru.rows{jj};
    
    if Nmod==1 
        tempMat=tempMat(:); 
    end 
    

    [~,~,~]=plot3sampSub(inStru.xaxis,tempMat,tempTit,1,inStru);
        
    if jj==NOuter || ISubplots==NRows*NCols;
        ISubplots=0;
        % Flush Suptitle?
        %tempSuptit='Transitions'; 
        tempSuptit=''; 
        if isempty(inStru.suptitle)==false;
            if strmatch(lower(inStru.suptitleFlush),'left')==1;
                tempSuptit=[char(inStru.suptitle),char(tempSuptit)];
            else
                tempSuptit=[char(tempSuptit),char(inStru.suptitle)];
            end
            if size(graphsVec,1)==1
                supHand=suptitle(tempSuptit);
            else
                supHand=suptitle([tempSuptit,'   (',num2str(IGraphs),' of ',...
                    num2str(size(graphsVec,1)),')']);
            end
            set(supHand,'FontSize',inStru.suptitleFontSize,'FontName',inStru.FontName,...
                'FontWeight','Bold');
        end
        if  inStru.flagLandscape==1
            set(graphsVec(IGraphs,1),'PaperOrientation','Landscape')
        end
        set(graphsVec(IGraphs,1),'PaperPosition',inStru.paperPosition,'PaperUnits','in',...
            'PaperPositionMode','manual')
    end
end
if ~isempty(inStru.axesFontSize);
    set(0,'DefaultAxesFontSize','remove');
end

end 